package com.example.java.designpattern;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author zengty
 * @apiNote 委派模式
 * @date 2024/8/29
 * @project study
 */
public class DelegateDivisor implements Divisor {

    private final Collection<Divisor> divisors;

    public DelegateDivisor(){
        this.divisors = new ArrayList<>();
    }

    public DelegateDivisor(Collection<Divisor> divisors) {
        this.divisors = divisors;
    }

    public void addDivisor(Divisor divisor){
        divisors.add(divisor);
    }

    @Override
    public void divide(String s) {
        for (Divisor divisor : divisors) {
            if (divisor.isSupport(s)) {
                divisor.divide(s);
                return;
            }
        }
        throw new UnsupportedOperationException("Divisor " + s + " not supported");
    }
}
